# coding: utf-8
from redis import StrictRedis
from pymongo import MongoClient
import json


class Mafengwo:
    def __init__(self):
        self.sr = StrictRedis()
        self.mc = MongoClient('localhost', 27017)
        self.db = self.mc['mafengwo']
        self.col = self.db['mafengwo']
        self.rediskey = 'mafengwo'
        self.filename = 'ext/E2_mafengwo_error.json'

    def run(self):
        while True:
            _source, _data = self.sr.blpop(self.rediskey)
            _str_data = _data.decode().replace("'", "\"").replace("\\xa0", "").replace("\\t", "").replace("\\n", "")
            _json_data = "".join(_str_data.split())
            try:
                _dict_data = json.loads(_json_data)
                self.col.insert(_dict_data)
            except json.JSONDecodeError as e:
                print(e)
                with open(self.filename, 'w+', encoding='utf-8') as f:
                    f.write(_json_data)


def main():
    mfw = Mafengwo()
    mfw.run()


if __name__ == '__main__':
    main()
